﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using PersonnelStatistics.Entities;
using PersonnelStatistics.BLL;

namespace 人员统计
{
    public partial class Form2 : Form
    {
        private readonly PersonalService _personalSevice;
        public Form2()
        {
            InitializeComponent();
            _personalSevice = new PersonalService();
        }


        private void CloseForm()
        {
            this.Hide();
            this.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var Zp = "无";

            ////获取应用程序的当前工作目录
            string currentDirectory = System.IO.Directory.GetCurrentDirectory();
            var path = Path.Combine(currentDirectory, "Image");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (pictureBox1.Image != null)
            {
                //取得PictureBox中的图片对象
                Image imgSave = pictureBox1.Image;
                //保存到本地，路径可自行设置
                string Rtime = Convert.ToString(DateTime.Now.Ticks);

                string saveFilename = path + "\\Image\\" + textBox1.Text + Rtime + ".jpg";

                imgSave.Save(saveFilename);

                Zp = textBox1.Text + Rtime + ".jpg";
            }

            //添加数据
            var entity = new Personal
            {
                姓名 = this.textBox1.Text,
                性别 = this.comboBox1.Text,
                年龄 = Convert.ToInt16(this.numericUpDown1.Value.ToString()),
                学历 = this.comboBox2.Text,
                部门 = this.comboBox3.Text,
                职位 = this.comboBox4.Text,
                工资 = this.textBox2.Text,
                政治面貌 = this.comboBox5.Text,
                婚姻 = this.comboBox6.Text,
                身份证 = this.textBox3.Text,
                住址 = this.textBox4.Text,
                电话 = this.textBox5.Text,
                入职时间 = this.textBox8.Text,
                离职时间 = "",
                备用联系人 = this.textBox6.Text,
                联系人电话 = this.textBox7.Text,
                照片 = Zp
            };
            try
            {
                _personalSevice.AddOrUpdate(entity);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            var dialogResult = MessageBox.Show("新数据： " + textBox1.Text + "  已经成功保存！是否继续添加新数据？---点击【取消】返回主窗口---",
                "保存数据提示",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question);
            if (dialogResult == DialogResult.OK)
            {
                RestForm();
            }
            else
            {
                CloseForm();
            }

        }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            textBox8.Text = dateTimePicker1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "所有文件(*.*)|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.ImageLocation = dlg.FileName.ToString();
            }
        }

        private void Form2_Load_1(object sender, EventArgs e)
        {
            textBox8.Text = DateTime.Now.ToLongDateString();
            RestForm();
        }

        /// <summary>
        /// 重置(清空)表单
        /// </summary>
        private void RestForm()
        {
            textBox1.Focus();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
        }
    }
}

//INSERT INTO [dbo].[Table] ([Id], [姓名], [性别], [年龄], [学历], [部门], [职位], [工资], [政治面貌], [婚姻], [身份证], [住址], [电话], [入职时间], [离职时间], [备用联系人], [联系人电话], [照片]) VALUES (0, N'刘德华', N'男', 20, 大学, N'生产部',N'主管', 3000,N'党员',N'未婚','432522201011201856', N'广东省深圳市福田区', '13318652233', '2020/10/20', NULL, N'爸爸', '18817589988', <SQLVARIANT>)
